package edu.hcp.longshorturl.controller;

import edu.hcp.longshorturl.entity.CustomEntity;
import edu.hcp.longshorturl.entity.UrlEntity;
import edu.hcp.longshorturl.service.ICustomService;
import edu.hcp.longshorturl.service.IUrlService;
import edu.hcp.longshorturl.utils.UrlUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**                                                                                ____________________
      _                _                                                           < 神兽护体，永无bug! >
    | |__  _   _  ___| |__   ___ _ __   __ _ _ __   ___ _ __   __ _                --------------------
   | '_ \| | | |/ __| '_ \ / _ \ '_ \ / _` | '_ \ / _ \ '_ \ / _` |                       \   ^__^
  | | | | |_| | (__| | | |  __/ | | | (_| | |_) |  __/ | | | (_| |                        \  (oo)\_______
 |_| |_|\__,_|\___|_| |_|\___|_| |_|\__, | .__/ \___|_| |_|\__, |                           (__)\       )\/\
                                   |___/|_|                |___/                                ||----w |
                                                                                                ||     ||
 * @author huchengpeng
 * @date 2021/8/21 19:27
 * @version
 * @Description
 */
@Controller
public class Urlcontroller {
    @Autowired
    private IUrlService urlService;

    @Autowired
    private ICustomService customService;


    @GetMapping("/long2short")
    @ResponseBody
    public String longToshortUrl(@RequestParam String longUrl){
        UrlEntity urlEntity = new UrlEntity();
        urlEntity.setLongUrl(longUrl);
        UrlEntity isExist = urlService.queryByLongUrl(longUrl);
        if( isExist != null){
            return UrlUtils.idToShortUrl(isExist.getId());
        }
        boolean save = urlService.insertLongUrl(urlEntity);
        if(save){
            Long id = urlEntity.getId();
            return UrlUtils.idToShortUrl(id);
        }
        return "出错了！";
    }

    @GetMapping("/{shortUrl}")
    public String shortTolongUrl(@PathVariable String shortUrl, HttpServletResponse response){
        String longUrl;
        //先去custom中查找此短链，判断是否是自定义的
        final CustomEntity customEntity = customService.queryByShortUrl(shortUrl);
        if(customEntity != null){
            longUrl = customEntity.getLongUrl();
        }else {
            longUrl = urlService.shortTolong(shortUrl);
        }
        response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
        response.setHeader("Location", longUrl);
        response.setHeader("Connection", "close");
        return "redirect:"+longUrl;
    }

    @PostMapping("/customUrl")
    public String customUrl(CustomEntity customUrl){
        if(StringUtils.isBlank(customUrl.getCustomUrl()) || StringUtils.isBlank(customUrl.getLongUrl())){
            return "参数不合法";
        }
        final boolean success = customService.insertCustomUrl(customUrl);
        if(success){
            return "成功";
        }
        return "失败";
    }

}

